package com.itxx.thread7;
//静态同步方法
public class TicketDemo implements Runnable {
    private static int sticket = 100;

    @Override
    public void run() {
        mothed();
    }

    private static synchronized void mothed() {
        while (true) {
            synchronized (TicketDemo.class) {
                if (sticket == 0) {
                    System.out.println(Thread.currentThread().getName() + ":票卖完了");
                    break;
                } else if (sticket > 0) {
                    try {
                        Thread.sleep(10);
                        System.out.println(Thread.currentThread().getName() + "正在卖" + sticket + "张票");
                        sticket--;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

class Demo {
    public static void main(String[] args) {
        TicketDemo tt = new TicketDemo();
        Thread t1 = new Thread(tt, "窗口1");
        Thread t2 = new Thread(tt, "窗口2");
        Thread t3 = new Thread(tt, "窗口3");
        t1.start();
        t3.start();
        t2.start();
    }
}

